package logic.objects;

import java.util.ArrayList;

public class Problem {
	private ArrayList<Node> nodes;
	private ConnectionArrayList connections;
	public boolean color;
	private Double bestTourSoFarValue;
	private ArrayList<Node> bestTourSoFar;

	public Problem(boolean color) {
		nodes = new ArrayList<Node>();
		connections = new ConnectionArrayList();
		this.color = color;
	}

	public void addNode(int x, int y) {
		Node node = new Node(x, y);
		addCreatedNode(node);
		applyConnections();
	}

	private void addCreatedNode(Node newNode) {
		nodes.add(newNode);
		int index = nodes.indexOf(newNode);
		newNode.setId(index);

	}

	@SuppressWarnings("unchecked")
	public synchronized ArrayList<Node> getNodes() {
		return (ArrayList<Node>) nodes.clone();
	}

	public synchronized ConnectionArrayList getConnections() {
		return connections;
	}

	public synchronized void applyConnections() {
		connections = new ConnectionArrayList();
		for (Node applynode : nodes) {
			int applyNodeIndex = nodes.indexOf(applynode);
			int nodeIndex;
			for (Node node : nodes) {
				if (applynode.getId() != node.getId()) {
					Connection con = new Connection(applynode, node);
					nodeIndex = nodes.indexOf(node);
					connections.add(con, applyNodeIndex, nodeIndex);
					connections.add(con, nodeIndex, applyNodeIndex);
				} else {
					connections.add(null, applyNodeIndex, applyNodeIndex);
				}
			}
		}
	}

	public synchronized void deleteAllNodes() {
		for (int i = nodes.size() - 1; i > -1; i--)
			nodes.remove(i);
	}

	public Double getBestTourSoFarValue() {
		return bestTourSoFarValue;
	}

	public void setBestTourSoFarValue(Double bestTourSoFarValue) {
		this.bestTourSoFarValue = bestTourSoFarValue;
	}

	public ArrayList<Node> getBestTourSoFar() {
		return bestTourSoFar;
	}

	public void setBestTourSoFar(ArrayList<Node> bestTourSoFar) {
		this.bestTourSoFar = bestTourSoFar;
	}

	public void reset() {
		setBestTourSoFar(null);
		setBestTourSoFarValue(null);
	}
}
